import re
from app.utils.tools import Tools

from typing import Optional
from pydantic import BaseModel, validator, conint


class Bill(BaseModel):
    name: str
    money: float
    fm_type: conint(le=1, ge=0)
    payment: str
    expenses_kind_id: Optional[str] = None
    remark: Optional[str] = None
    bill_time: str
    user_id: str


class BillUpdate(Bill):
    pass


class BillCreate(Bill):
    pass


class CategoryItem(BaseModel):
    name: str


class CategoryUpdate(CategoryItem):
    pass


class CategoryCreate(CategoryItem):
    pass


class VideoItem(BaseModel):
    title: str
    sub_title: Optional[str] = None
    synopsis: Optional[str] = None
    remark: Optional[str] = None
    source_id: str
    category_id: str
    user_id: str


class VideoUpdate(Bill):
    pass


class VideoCreate(Bill):
    pass


class User(BaseModel):
    password: str

    @validator('password')
    def password_check(cls, v):
        if not re.match(r"^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).{8,1280}$", v):
            raise ValueError("Bad password format!")
        return v


class LoginItem(User):
    username: str
    access_id: str


class RegisterItem(User):
    phone: str

    # @validator('nickname')
    # def nickname_check(cls, v):
    #     if Tools.is_number(v):
    #         raise ValueError("Nickname can not be pure numbers")
    #     return v

    # @validator('email')
    # def email_check(cls, v):
    #     if not re.match(r"^[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$", v):
    #         raise ValueError("Bad email format!")
    #     return v

    @validator('phone')
    def phone_check(cls, v):
        if not re.match(r"^1[3578]\d{9}$|^147\d{8}$|^179\d{8}$", v):
            raise ValueError("Bad phone format!")
        return v


class Barrage(BaseModel):
    color: int
    text: str
    type: int
    author: str
    id: str
    time: float
